/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zwu.tomcatspace;

import java.io.File;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.Workspace;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import javax.jcr.query.RowIterator;
import org.apache.jackrabbit.core.TransientRepository;

/**
 *
 * @author zwu
 */
public class ExceptSQL {

    public static void main(String[] args) throws Exception {

        System.out.println(new File(".").getAbsolutePath());
        Repository repository = new TransientRepository(new File("data/tomcat"));
        Session session = repository.login(
                new SimpleCredentials("username", "password".toCharArray()));


        try {
            //testQueryExcept(session.getWorkspace());
            testQuery(session.getWorkspace());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            session.logout();
        }

    }

    /**
     * A good one.
     * @param wk
     * @throws Exception
     */
    public static void testQueryExcept(Workspace wk) throws Exception {
        QueryManager qm = wk.getQueryManager();
        //String query = "select excerpt(.) from nt:resource where contains(., 'zhou')";
        String excerptQuery = "select excerpt(.) from nt:resource where contains(., 'zhou') and jcr:path ='%s'";
        //Query q= qm.createQuery(query, Query.SQL);

        String query = "select * from [nt:resource] as file where contains(file.*, 'zhou')  ";
        Query q = qm.createQuery(query, Query.JCR_SQL2);



        QueryResult result = q.execute();
        for (RowIterator it = result.getRows(); it.hasNext();) {
            Row r = it.nextRow();

            Node nn = r.getNode();
            String exq = String.format(excerptQuery, nn.getPath());
            System.out.println(qm.createQuery(exq, Query.SQL).execute().getRows().nextRow().getValue("rep:excerpt(.)").getString());
            System.out.println(nn.getParent().getPath());

            PropertyIterator pi2 = nn.getProperties();
            while (pi2.hasNext()) {
                System.out.println(pi2.nextProperty().getName());
            }
            // nn.getParent().remove();

            //Value excerpt = r.getNode("file").g("rep:excerpt(file)");
            //System.out.print(excerpt.getString());
        }


        //Thread.currentThread().sleep(10000);
    }

    public static void testQuery(Workspace wk) throws Exception {
        QueryManager qm = wk.getQueryManager();
        //String query = "select excerpt(.) from nt:resource where contains(., 'us')";
        String query = "select file.[jcr:uuid] from [nt:resource] as file where contains(text(file.[jcr:data]), 'user')  ";

        Query q = qm.createQuery(query, Query.JCR_SQL2);
        QueryResult result = q.execute();
        for (RowIterator it = result.getRows(); it.hasNext();) {
            Row r = it.nextRow();
           
            Node nn = r.getNode();
            System.out.println(nn.getParent().getPath());
            PropertyIterator pi2 = nn.getProperties();
            while (pi2.hasNext()) {

                Property p = pi2.nextProperty();
                System.out.println(p.getName() + "..."+p.getValue().getString() );
            }

           
           //System.out.println(r.getValue("rep:excerpt(.)").getString());
            // nn.getParent().remove();

            //Value excerpt = r.getNode("file").g("rep:excerpt(file)");
            //System.out.print(excerpt.getString());
        }


        //Thread.currentThread().sleep(10000);
    }
}
